var tgd = tgd || {};
tgd.Contests = tgd.Contests || {};
tgd.Contests.SameGame = tgd.Contests.SameGame || {};
tgd.Contests.SameGame.Solver = tgd.Contests.SameGame.Solver || {};
tgd.Contests.SameGame.View = tgd.Contests.SameGame.View || {};

tgd.Contests.SameGame.Solver.Max = function (game) {
	var _game = game;
	var max = 0;
	document.getElementById("panFields").appendChild(new tgd.Contests.SameGame.View.Field(_game.Field.Grid));

	var self = this;
	var runTimer = function() {
		if (_game.Field.HasAvailableGroup) {
			_game.Field.selectMax();
		} else {
			console.log("Game solver finished: " + _game.Score)
			if (_game.Score > max) {
				max = _game.Score;
				document.getElementById("lblScore").textContent = _game.Score;
				document.getElementById("txtSolution").textContent = _game;
				document.getElementById("panFields").appendChild(new tgd.Contests.SameGame.View.Field(_game.Field.Grid));
			}
			_game.reset();
		}
		setTimeout(runTimer, 0);
	}
	setTimeout(runTimer, 32);
}